package com.kobe.algorithm.leetcode.case4;

class Solution {

    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int length = nums1.length +nums2.length;
        int num1Index = 0;
        int num2Index = 0;
        int [] total = new int[length];
        if(nums1 == null || nums1.length == 0){
            total = nums2;
        }else if(nums2 == null || nums2.length == 0){
            total = nums1;
        }else{
            for(int i = 0; i < length; i++){
                if(num1Index > nums1.length-1 && num2Index <= nums2.length-1){
                    total[i] = nums2[num2Index];
                    num2Index++;
                    continue;
                }

                if(num2Index > nums2.length-1 && num1Index <= nums1.length-1){
                    total[i] = nums1[num1Index];
                    num1Index++;
                    continue;
                }
                if(nums1[num1Index] <= nums2[num2Index] ){
                    total[i] = nums1[num1Index];
                    num1Index++;
                }else{
                    total[i] = nums2[num2Index];
                    num2Index++;
                }
            }
        }

        if(length%2 == 0){
            return  (Double.valueOf(total[length/2-1]) + Double.valueOf(total[length/2])) /2;
        }else{
            return Double.valueOf(total[length/2]);
        }
    }
}
